package org.jeecg.glr.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.glr.entity.GlrVideo;
import org.jeecg.glr.entity.GlrVideoBdRecognize;
import org.jeecg.glr.service.IGlrVideoBdRecognizeService;
import org.jeecg.glr.service.IGlrVideoService;
import org.jeecg.glr.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 海康摄像头人脸识别结果
 * @Author: jeecg-boot
 * @Date:   2020-02-11
 * @Version: V1.0
 */
@RestController
@RequestMapping("/glr/glrVideoBdRecognize")
@Slf4j
public class GlrVideoBdRecognizeController extends JeecgController<GlrVideoBdRecognize, IGlrVideoBdRecognizeService> {
	@Autowired
	private IGlrVideoBdRecognizeService glrVideoBdRecognizeService;
	
	/**
	 * 分页列表查询
	 *
	 * @param glrVideoBdRecognize
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@GetMapping(value = "/list")
	public Result<?> queryPageList(GlrVideoBdRecognize glrVideoBdRecognize,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {

		String stscoreString=glrVideoBdRecognize.getScoreString();
		if (StringUtil.isNotEmpty(stscoreString)){
			glrVideoBdRecognize.setScoreString(null);
		}
		QueryWrapper<GlrVideoBdRecognize> queryWrapper = QueryGenerator.initQueryWrapperCode(glrVideoBdRecognize, req.getParameterMap());
		if (StringUtil.isNotEmpty(stscoreString)){
			stscoreString=stscoreString.replace("*","");
			double d1=Double.parseDouble(stscoreString);
			queryWrapper.ge("score",d1);
		}

		Page<GlrVideoBdRecognize> page = new Page<GlrVideoBdRecognize>(pageNo, pageSize);
		IPage<GlrVideoBdRecognize> pageList = glrVideoBdRecognizeService.page(page, queryWrapper);
		List<GlrVideoBdRecognize> list=pageList.getRecords();
		for (GlrVideoBdRecognize res :
				list) {
			if(StringUtil.isNotEmpty(res.getCatalog())){
				res.setFilePath(res.getCatalog());
			}

		}

		return Result.ok(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param glrVideoBdRecognize
	 * @return
	 */
	@AutoLog(value = "添加海康摄像头人脸识别结果",logType= CommonConstant.LOG_TYPE_2)
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody GlrVideoBdRecognize glrVideoBdRecognize) {
		glrVideoBdRecognizeService.save(glrVideoBdRecognize);
		return Result.ok("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param glrVideoBdRecognize
	 * @return
	 */
	@AutoLog(value = "修改海康摄像头人脸识别结果",logType= CommonConstant.LOG_TYPE_2)
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody GlrVideoBdRecognize glrVideoBdRecognize) {
		glrVideoBdRecognizeService.updateById(glrVideoBdRecognize);
		return Result.ok("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "删除海康摄像头人脸识别结果",logType= CommonConstant.LOG_TYPE_2)
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		glrVideoBdRecognizeService.removeById(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "批量删除海康摄像头人脸识别结果",logType= CommonConstant.LOG_TYPE_2)
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.glrVideoBdRecognizeService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		GlrVideoBdRecognize glrVideoBdRecognize = glrVideoBdRecognizeService.getById(id);
		if(glrVideoBdRecognize==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(glrVideoBdRecognize);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param glrVideoBdRecognize
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, GlrVideoBdRecognize glrVideoBdRecognize) {
        return super.exportXls(request, glrVideoBdRecognize, GlrVideoBdRecognize.class, "音柱管理");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, GlrVideoBdRecognize.class);
    }

}
